package com.cloud.school.app.config.security;


import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class SecurityUserDetails implements UserDetails {

    /**
     * @Fields serialVersionUID : serialVersionUID
     */
    private static final long serialVersionUID = -2015508451231680464L;
    /**
     * 用户id
     */
    private Long userId;
    /**
     * 用户名
     */
    private String username;
    /**
     * 密码
     */
    private String password;

    private List<SecurityAuth> authorities;

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public void setPassword(String password) {
        this.password = "{noop}" + password;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setAuthorities(List<SecurityAuth> authorities) {
        this.authorities = authorities;
    }

    public void setRoles(String... roles) {
        this.authorities = Stream.of(roles).map(SecurityAuth::new).collect(Collectors.toList());
    }

    public void addRoles(String role) {
        if (this.authorities != null) {
            this.authorities.add(new SecurityAuth(role));
        } else {
            this.authorities = new ArrayList<>();
            this.authorities.add(new SecurityAuth(role));
        }
    }

    public SecurityUserDetails(Long userId, String username, List<SecurityAuth> authorities) {
        this.userId = userId;
        this.username = username;
        this.authorities = authorities;
    }

    public SecurityUserDetails(Long userId, String username, String password, Set<String> roles) {
        this.userId = userId;
        this.username = username;
        this.password = "{noop}" + password;
        this.authorities = roles.stream().map(SecurityAuth::new).collect(Collectors.toList());
    }

    public SecurityUserDetails(Long userId, String username, Set<String> roles) {
        this(userId, username, null, roles);
        this.password = null;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    public static class SecurityAuth implements GrantedAuthority {

        private String authority;

        @Override
        public String getAuthority() {
            return authority;
        }

        public SecurityAuth(String role) {
            this.authority = "ROLE_" + role;
        }

        public SecurityAuth() {
        }

        public void setAuthority(String authority) {
            this.authority = authority;
        }

    }
}
